package ipdesk.model.report.consult.dao;


import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.dao.client.DaoManager;
import com.opensymphony.xwork2.ActionSupport;

import ipdesk.common.Codes;
import ipdesk.model.report.consult.bean.ConsultInfoBean;

import ecg.dao.DaoService;

import ecg.util.FileService;
import ecg.util.SqlMapLocator;

public class ConsultInfoDao extends ActionSupport
{
	private static ConsultInfoDao singleton;
	
	protected ConsultInfoDao(){}
	
	public static ConsultInfoDao getInstance()
	{
		if(singleton == null)
		{
			singleton = new ConsultInfoDao();
		}
		
		return singleton;
	}
	
	private DaoManager	manager	= DaoService.getInstance();
	private IConsultInfo consultInfo 	= (IConsultInfo)manager.getDao(IConsultInfo.class, "ip_desk");
	
	/**
	 * 입력받은 데이터를 DB에 insert 하는 메서드.
	 */
	public int consultInfoReg(ConsultInfoBean consultInfoBean) throws Exception
	{
		int lastConsultingSeq = 0;
		try{
			manager.startTransaction();
			
			lastConsultingSeq = consultInfo.consultInfoReg(consultInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
		
		
		return lastConsultingSeq;
	}
	
	/**
	 * 파일 데이터 insert.
	 * 
	 */
	public void insertFileData(ConsultInfoBean consultInfoBean) throws Exception
	{
		try{
			manager.startTransaction();
			
			consultInfo.insertFileData(consultInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}		
		
	}
	
	public int getDataTotalCount(ConsultInfoBean consultInfoBean) throws Exception
	{
		int totalDataCount = consultInfo.getDataTotalCount(consultInfoBean);
		
		return totalDataCount;
	}
	
	/**
	 * 담당자 AND 센터에 해당하는 데이터를 가지고온다.
	 * 
	 * @param map
	 * @return
	 */
	public List<ConsultInfoBean> getPagingDataList(Map map) throws Exception
	{
		List<ConsultInfoBean> pageDataList = consultInfo.getPagingDataList(map);
		
		for(int i=0; i < pageDataList.size(); i++)
		{
			// 코드값을 코드에 해당하는 스트링으로 변환.
			String consultType = Codes.getInstance().getFromConsultingTypeCodeToValue(pageDataList.get(i).getConsultingTypeCode());
			String iprCodeValue = Codes.getInstance().getFromIprCodeToValue(pageDataList.get(i).getIprCode());
			String consultingWay = Codes.getInstance().getFromConsultingWayCodeToValue(pageDataList.get(i).getConsultingWayCode());
			
			// 변환된 스트링을 다시 빈에 저장.
			pageDataList.get(i).setConsultingTypeCode(consultType);
			pageDataList.get(i).setIprCode(iprCodeValue);
			pageDataList.get(i).setConsultingWayCode(consultingWay);
		}
		
		return pageDataList;
	}
	
	
	/**
	 *  해당 ConsultingSeq에 대한 데이터를 가지고 온다.
	 *
	 */
	public ConsultInfoBean getDetailData(int consultSeq)
	{
		ConsultInfoBean consultInfoBean = consultInfo.getDataOneRow(consultSeq, "consult.consultDetailData");
		
		// 코드값을 코드에 해당하는 스트링으로 변환.
		String consultType = Codes.getInstance().getFromConsultingTypeCodeToValue(consultInfoBean.getConsultingTypeCode());
		String consultSubType = Codes.getInstance().getFromConsultingSubTypeCodeToValue(consultInfoBean.getConsultingSubTypeCode());
		String iprCodeValue = Codes.getInstance().getFromIprCodeToValue(consultInfoBean.getIprCode());
		String consultingWay = Codes.getInstance().getFromConsultingWayCodeToValue(consultInfoBean.getConsultingWayCode());
		String consultingCategory = Codes.getInstance().getFromConsultingCategoryToValue(consultInfoBean.getConsultingCategory());
		String difficulty = consultInfoBean.getDifficulty().replace("\n", "<br/>");
		String consultingContent = consultInfoBean.getConsultingContent().replace("\n", "<br/>");
		String futurePlan = consultInfoBean.getFuturePlan().replace("\n", "<br/>");
		if(consultInfoBean.getModifyTime() != null && (!consultInfoBean.getModifyTime().equals("")))
		{
			String modifyTime = consultInfoBean.getModifyTime().substring(0, 10);
			consultInfoBean.setModifyTime(modifyTime);
		}
		
		// 변환된 스트링을 다시 빈에 저장.
		consultInfoBean.setConsultingTypeCode(consultType);
		consultInfoBean.setConsultingSubTypeCode(consultSubType);
		consultInfoBean.setIprCode(iprCodeValue);
		consultInfoBean.setConsultingWayCode(consultingWay);
		consultInfoBean.setConsultingCategory(consultingCategory);
		consultInfoBean.setDifficulty(difficulty);
		consultInfoBean.setConsultingContent(consultingContent);
		consultInfoBean.setFuturePlan(futurePlan);
		
		
		return consultInfoBean;
	}
	
	/**
	 *  해당 ConsultingSeq에 대한 데이터를 가지고 온다.
	 *
	 */
	public ConsultInfoBean getModifyData(int consultSeq)
	{
		ConsultInfoBean consultInfoBean = consultInfo.getDataOneRow(consultSeq, "consult.consultDetailData");
		
		// 코드값을 코드에 해당하는 심플코드로 변환.
		String consultTypeCodeSimple = Codes.getInstance().getFromConsultingTypeCodeToSimpleCode(consultInfoBean.getConsultingTypeCode());
		String consultSubTypeCodeSimple = Codes.getInstance().getFromConsultingSubTypeCodeToSimpleCode(consultInfoBean.getConsultingSubTypeCode());
		String iprCodeSimple = Codes.getInstance().getFromIprCodeToSimpleCode(consultInfoBean.getIprCode());
		String consultingWayCodeSimple = Codes.getInstance().getFromConsultingWayCodeToSimpleCode(consultInfoBean.getConsultingWayCode());
		String consultingCategorySimple = Codes.getInstance().getFromConsultingCategoryToSimpleCode(consultInfoBean.getConsultingCategory());
		
		// 변환된 스트링을 다시 빈에 저장.
		consultInfoBean.setConsultingTypeCode(consultTypeCodeSimple);
		consultInfoBean.setConsultingSubTypeCode(consultSubTypeCodeSimple);
		consultInfoBean.setIprCode(iprCodeSimple);
		consultInfoBean.setConsultingWayCode(consultingWayCodeSimple);
		consultInfoBean.setConsultingCategory(consultingCategorySimple);
		
		return consultInfoBean;
	}
	
	/**
	 *  해당 ConsultingSeq에 대한 파일데이터를 가지고 온다.
	 *
	 */
	public ConsultInfoBean getUploadData(int consultSeq)
	{
		ConsultInfoBean consultInfoBean = consultInfo.getDataOneRow(consultSeq, "consult.consultUploadData");
				
		return consultInfoBean;
	}
	
	/**
	 *  해당 ConsultingSeq에 대한 데이터를 삭제한다.
	 *
	 */
	public void deleteDataOneRow(int consultSeq)
	{
		consultInfo.deleteDataOneRow(consultSeq, "consult.deleteUploadData");
	}
	
	/**
	 *  해당 ConsultingSeq에 대한 데이터를 업데이트 한다.
	 */
	public int consultInfoUpdate(ConsultInfoBean consultInfoBean) throws Exception
	{
		// 심플코드값을 원래 코드로 변환.
		String consultTypeCode = Codes.getInstance().getFromSimpleCodeToConsultingTypeCode(consultInfoBean.getConsultingTypeCode());
		String consultSubTypeCode = Codes.getInstance().getFromSimpleCodeToConsultingSubTypeCode(consultInfoBean.getConsultingSubTypeCode());
		String iprCode = Codes.getInstance().getFromSimpleCodeToIprCode(consultInfoBean.getIprCode());
		String consultingWayCode = Codes.getInstance().getFromSimpleCodeToConsultingWayCode(consultInfoBean.getConsultingWayCode());
		String consultingCategoryCode = Codes.getInstance().getFromSimpleCodeToConsultingCategorye(consultInfoBean.getConsultingCategory());
		
		// 변환된 스트링을 다시 빈에 저장.
		consultInfoBean.setConsultingTypeCode(consultTypeCode);
		consultInfoBean.setConsultingSubTypeCode(consultSubTypeCode);
		consultInfoBean.setIprCode(iprCode);
		consultInfoBean.setConsultingWayCode(consultingWayCode);
		consultInfoBean.setConsultingCategory(consultingCategoryCode);
		
		int result = 0;
		
		try{
			manager.startTransaction();
			
			// 지재권 상담일지 데이터 업데이트.
			result = consultInfo.consultInfoUpdate(consultInfoBean, "consult.updateConsultData");
			
			manager.commitTransaction();
			
		}finally{
			manager.endTransaction();
		}
		
		
		return result;
	}
	
	
	/**
	 *  해당 ConsultingSeq에 대한 데이터를 업데이트 한다.
	 */
	public int consultInfoUpdateFile(ConsultInfoBean consultInfoBean) throws Exception
	{
		// 심플코드값을 원래 코드로 변환.
		String consultTypeCode = Codes.getInstance().getFromSimpleCodeToConsultingTypeCode(consultInfoBean.getConsultingTypeCode());
		String consultSubTypeCode = Codes.getInstance().getFromSimpleCodeToConsultingSubTypeCode(consultInfoBean.getConsultingSubTypeCode());
		String iprCode = Codes.getInstance().getFromSimpleCodeToIprCode(consultInfoBean.getIprCode());
		String consultingWayCode = Codes.getInstance().getFromSimpleCodeToConsultingWayCode(consultInfoBean.getConsultingWayCode());
		String consultingCategoryCode = Codes.getInstance().getFromSimpleCodeToConsultingCategorye(consultInfoBean.getConsultingCategory());
		
		
		// 변환된 스트링을 다시 빈에 저장.
		consultInfoBean.setConsultingTypeCode(consultTypeCode);
		consultInfoBean.setConsultingSubTypeCode(consultSubTypeCode);
		consultInfoBean.setIprCode(iprCode);
		consultInfoBean.setConsultingWayCode(consultingWayCode);
		consultInfoBean.setConsultingCategory(consultingCategoryCode);
		
		int result = 0;
		
		try{
			manager.startTransaction();
			
			// 지재권 상담일지 데이터 업데이트.
			result = consultInfo.consultInfoUpdate(consultInfoBean, "consult.updateConsultDataFile");
			
			
			manager.commitTransaction();
			
		}finally{
			manager.endTransaction();
		}
		
		
		return result;
	}
	
	/**
	 * 파일 삭제
	 */
	public boolean fileDelete(int consultingSeq)
	{
		boolean fileDeleteFlag = true; 
		boolean deleteFlag = false;
		
		// consultingSeql에 해당하는 파일 데이터를 가지고 온다.
		//ConsultInfoBean consultInfoBean = consultInfo.getDataOneRow(consultingSeq, "consult.consultUploadData");
		
		// 데이터가 존재한다면 데이터 삭제. 파일 삭제
		/*if(consultInfoBean != null)
		{
			try{
				manager.startTransaction();
				// 파일 데이터 삭제
				consultInfo.deleteDataOneRow(consultingSeq, "consult.deleteUploadData");
				manager.commitTransaction();
				
				String filePathPrefix = getText("path.upload.prefix");
				String filePath = consultInfoBean.getPathName() + consultInfoBean.getUploadFileName();
				// 파일 삭제
				FileService fs = new FileService();
				fs.deleteFile(filePathPrefix + filePath);
				
				deleteFlag = true;
			}finally{
				manager.endTransaction();
			}
		}*/
		consultInfo.deleteFileData(consultingSeq, "consult.deleteUploadData");
		
		return fileDeleteFlag;
	}
	
	
	/**
	 * 데이터 삭제
	 * 
	 */
	public boolean consultInfoDelete(int consultingSeq)
	{
		
		boolean fileDeleteFlag = false; 
		boolean deleteFlag = false;
		
		try{
			manager.startTransaction();
			// 파일 테이블 로우 및 파일 데이터 삭제.
			fileDeleteFlag = this.fileDelete(consultingSeq);
			
			// consult 데이터 삭제.
			consultInfo.deleteDataOneRow(consultingSeq, "consult.deleteConsultData");
				
			deleteFlag = true;
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
		
		return deleteFlag;
	}
	
	
}
